package com.yanwq.pulltorefreshview.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.View;

/**
 * 作者：yanweiqiang。
 * 日期：2016/7/13 0013。
 */
public class PullToRefreshProgressView extends View {
    private Paint mPaint;
    private RectF mRectF;
    private int mStartAngle;
    private int mSwapAngle;
    private int mStroke;
    private Status mStatus;
    private boolean mClockwise;

    enum Status {
        prepare_start,
        started,
        prepare_stop,
        stopped,
        paused
    }

    public PullToRefreshProgressView(Context context) {
        super(context);
        init();
    }

    public PullToRefreshProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PullToRefreshProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mStartAngle = 0;
        mSwapAngle = 90;
        mStroke = 10;
        mPaint = new Paint();
        mPaint.setColor(0xff53cfd0);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStroke);
        mPaint.setAntiAlias(true);
        mStatus = Status.prepare_start;
        mRectF = new RectF((getWidth() - getHeight()) / 2 + mStroke, mStroke, (getWidth() + getHeight()) / 2 - mStroke, getHeight() - mStroke);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mRectF.left = (getWidth() - getHeight()) / 2 + mStroke;
        mRectF.top = mStroke;
        mRectF.right = (getWidth() + getHeight()) / 2 - mStroke;
        mRectF.bottom = getHeight() - mStroke;

        canvas.drawArc(mRectF, mStartAngle, mSwapAngle, false, mPaint);
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mStatus == Status.paused) {
            startProgress();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mStatus = Status.paused;
    }

    public void updateCircle(int startAngle, int swapAngle) {
        mStartAngle = startAngle;
        mSwapAngle = swapAngle;
        invalidate();
    }

    public void stopProgress() {
        mStatus = Status.prepare_stop;
    }


    public void startProgress() {
        mStatus = Status.started;
        mClockwise = true;

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (mStatus == Status.started) {

                    try {
                        Thread.sleep(4);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    mStartAngle = (mStartAngle + 2) % 360;

                    if (mSwapAngle > 20 && mClockwise) {
                        mSwapAngle = mSwapAngle - 1;
                    } else {
                        mClockwise = false;
                    }

                    if (mSwapAngle < 200 && !mClockwise) {
                        mSwapAngle = mSwapAngle + 1;
                    } else {
                        mClockwise = true;
                    }

                    postInvalidate();
                }

                while (mStatus == Status.prepare_stop) {
                    try {
                        Thread.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    mSwapAngle += 2;
                    mSwapAngle = mSwapAngle > 360 ? 360 : mSwapAngle;

                    if (mSwapAngle == 360) {
                        mStatus = Status.stopped;
                    }

                    postInvalidate();
                }
            }
        }).start();
    }
}
